﻿
Imports System.Object
Imports VSModel
Imports Cls_Common
Imports System.IO
Imports System.Data
Partial Class FrontEnd_Control_Store_StoreList
    Inherits System.Web.UI.UserControl

#Region "Sub and function"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'xu ly jquery khi post back
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            Bind2Grid()
            BindToGrid(1)
        End If
    End Sub
    Private Sub Bind2Grid()
        Using Data As New VSEntities
            Dim k = (From q In Data.Categories Order By q.CatId, q.Title).ToList
            With ddlNganhNgheKinhDoanh
                .DataTextField = "Title"
                .DataValueField = "CatId"
                .DataSource = k
                .DataBind()
                .Items.Insert(0, New ListItem("--- Tất cả ---", "-1"))
            End With

            Dim pro = (From q In Data.Provinces Where q.ParentId = 171 Order By q.Title Select q).ToList
            With ddlHuyen
                .DataTextField = "Title"
                .DataValueField = "Id"
                .DataSource = pro
                .DataBind()
                .Items.Insert(0, New ListItem("--- Tất cả ---", "-1"))
            End With
        End Using
    End Sub
    Private Sub BindToGrid(Optional ByVal iPage As Integer = 1 _
                        , Optional ByVal strHokinhdoanh As String = "" _
                        , Optional ByVal strNguoidaidien As String = "" _
                        , Optional ByVal strDiachi As String = "" _
                        , Optional ByVal strLinhvuc As Integer = -1 _
                        , Optional ByVal intTinhthanhId As Integer = -1)
        Using Data As New VSEntities
            Dim arrSearch() As String = {strHokinhdoanh.ToString, strNguoidaidien.ToString, strDiachi.ToString, strLinhvuc.ToString, intTinhthanhId.ToString}
            Dim count As Integer
            ViewState("search") = arrSearch
            'So ban ghi muon the hien tren trang
            Dim intPag_Size As Int32 = 10
            Dim p As List(Of uspHoCaTheSelectWeb_Result) = Data.uspHoCaTheSelectWeb(strHokinhdoanh _
                                                                                    , strNguoidaidien _
                                                                                    , strDiachi _
                                                                                    , strLinhvuc _
                                                                                    , intTinhthanhId _
                                                                                    , iPage, intPag_Size).ToList
            count = p.Count

            Dim strKey_Name() As String = {"StoreId", "Title"}

            'Dim p = (From q In Data.Stores
            '         Where q.StatusId = 3 And ((q.PublishedDate <= Now() And (q.eEffectiveDate >= Now()) Or q.eEffectiveDate Is Nothing))
            '         Order By q.PublishedDate Descending Select q).ToList

            'Tong so ban ghi
            lblTotal2.Text = count
            If p.Count > 0 Then
                hidCount.Value = p.FirstOrDefault.Total()
                Create_Pager(hidCount.Value, iPage, intPag_Size, 10)
            Else
                hidCount.Value = 0
                With rptPage
                    .DataSource = Nothing
                    .DataBind()
                End With
            End If

            With rptShow
                .DataSource = p
                .DataBind()
            End With
        End Using
    End Sub
    Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub

    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)
        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.Text = "<span class='current'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub

    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        Dim arrSearch() As String
        arrSearch = ViewState("search")
        BindToGrid(CInt(lnkTile.ToolTip) _
       , arrSearch(1) _
       , arrSearch(2) _
       , arrSearch(3) _
       , arrSearch(4) _
       , arrSearch(5))
        lnkLast.Enabled = True
        lnkFirst.Enabled = True
        If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf CInt(lnkTile.ToolTip) = 1 Then
            lnkFirst.Enabled = False
        End If
    End Sub
    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click
        If hidCur_Page.Value > 1 Then
            hidCur_Page.Value = hidCur_Page.Value - 1
        End If
        BindToGrid(hidCur_Page.Value)
    End Sub
    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        BindToGrid(hidCur_Page.Value)
    End Sub
#End Region
#Region "Event for control"
    Protected Sub rptShow_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptShow.ItemDataBound
        Dim row = CType(e.Item.DataItem, Object)
        'load img thumbalmail
        Dim img As Image = CType(e.Item.FindControl("img"), Image)
        img.ImageUrl = CookedImageURL("78-59", row.ImgLogo)
        img.AlternateText = row.Title
        'load Title
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.Text = row.Title
        hplTitle.NavigateUrl = MainDomain & "ho-kinh-doanh/" & row.QueryString & "-" & row.StoreId.ToString & ".html"

        'load hyperlink wrapper
        Dim hplWrapper As HyperLink = CType(e.Item.FindControl("hplWrapper"), HyperLink)
        hplWrapper.NavigateUrl = MainDomain & "ho-kinh-doanh/" & row.QueryString & "-" & row.StoreId.ToString & ".html"


        'load Description
        Dim ltrSlogan As Literal = CType(e.Item.FindControl("ltrSlogan"), Literal)
        ltrSlogan.Text = row.Slogan

        'load Address
        Dim ltrAddress As Literal = CType(e.Item.FindControl("ltrAddress"), Literal)
        ltrAddress.Text = row.DiaChi
    End Sub
#End Region

    Protected Sub btnFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFilter.Click
        BindToGrid(1, txtTenHoKinhDoanh.Text.Trim(), txtNguoiDaiDien.Text.Trim(), txtDiaChi.Text.Trim(), ddlNganhNgheKinhDoanh.SelectedValue, ddlHuyen.SelectedValue)
    End Sub

    Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtTenHoKinhDoanh.Text = ""
        txtNguoiDaiDien.Text = ""
        txtDiaChi.Text = ""
        ddlNganhNgheKinhDoanh.SelectedValue = -1
        ddlHuyen.SelectedValue = -1
        BindToGrid(1)
    End Sub
End Class
